<?php

/**
 * Created by IntelliJ IDEA.
 * User: keke
 * Date: 18-10-27
 * Time: 上午11:54
 */
require_once "../congfig/config.php";
require_once "../lib/common.php";
@$pName = $_POST['pName'];
@$pChar = $_POST['pChar'];
@$teacher = $_POST['teacher'];
@$pDesc = $_POST['pDesc'];
@$pRank = $_POST['pRank'];
@$file = $_FILES['Enclosure'];

if ($pName == null || $pChar == null || $teacher == null || $pDesc == null || $file == null) {
    exit(jsoncall(-1, "信息不能为空"));
}
$sql = "select * from proinfor where pName = '$pName'";
$query = $conn->query($sql);
$num = $query->num_rows;
if ($num >= 1) {
    exit(jsoncall(-1, "项目已经存在"));
}
$sql = "select * from users where username = '$pChar'";
$query = $conn->query($sql);
$row = $query->fetch_array();
$uid = $row['uid'];
$file = uploadfile($file, maxSize, filepath);
if($file===false){
    exit(jsoncall(-1,"移动失败"));
}
// $sql = "INSERT INTO proinfor VALUES
//  (null ,'$pName','$pChar','$teacher','$pDesc',current_date(),'$pRank','$file' )";
$sql = "insert into proinfor values 
(null,'$pName','$pChar' ,'$teacher' ,'$pDesc',current_date(),'$pRank','$file')";
$query = $conn->query($sql);
if(!$query){
    exit(jsoncall(-1, "添加项目失败"));
}
$sql = "select * from proinfor where pName = '$pName'"; 
$query = $conn->query($sql);
$row = $query->fetch_array();
$pid = $row['id'];
$sql = "insert into puser values (null,'$uid','$pid')";
$query = $conn->query($sql);
if ($query) {
    exit(jsoncall(1, "添加项目成功"));
} else {
    exit(jsoncall(-1, "添加项目失败"));
}